<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');

/**
 * Contao Open Source CMS
 * Copyright (C) 2005-2011 Leo Feyer
 *
 * Formerly known as TYPOlight Open Source CMS.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 * @copyright  2011 Andreas Koob 
 * @author     Andreas Koob 
 * @package    tasks 
 * @license    LGPL 
 * @filesource
 */


/**
 * Class tasklist 
 *
 * @copyright  2011 Andreas Koob 
 * @author     Andreas Koob 
 * @package    Controller
 */
class tm_tasklist extends ContentElement
{

	/**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'tm_tasklist';
	private $function;
	private $member = array();
	
	public function generate()
	{
		if (TL_MODE == 'BE')
		{
			$return="Tasks - Tasks list";
			return $return;
		}
		return parent::generate();
	}

	/**
	 * Generate module
	 */
	protected function compile()
	{
		$this->functions = new tm_functions();
		$this->member=$this->functions->getMemberInformation();
		
		$errors=array();
		if($this->Input->post('submit'))
		{
			if($this->Input->post('mode')=='new')
			{
				$this->functions->createTask($this->Input->post('projectid'),
											 $this->Input->post('title'),
											 $this->Input->post('description'),
											 $this->Input->post('task_type'),
											 $this->member['id']);
			}	
		}
		$this->Template->errors=$errors;
		
		if($this->tsk_use_fixed_project=='1'){
			$projectid = $this->tm_fixed_project;
		}
		else
		{
			$projectid = $this->Input->get('project');
		}
		$objProject = $this->Database->prepare("SELECT * FROM tl_tm_projects WHERE id=?")->execute($projectid);
		
		$objTasks = $this->Database->prepare("SELECT * FROM tl_tm_tasks WHERE pid=? AND deleted!=?")->execute($projectid,'1');
		
		
		
		$objTargetPage = $this->Database->prepare("SELECT id, alias FROM tl_page WHERE id=?")
												->limit(1)
												->execute($this->tm_redirect_task);
		while($objTasks->next()){
				$objLastcomment = $this->Database->prepare("SELECT *,count(c.id) as cnt_comment FROM tl_tm_comments as c INNER JOIN tl_member as m ON c.created_user=m.id WHERE c.pid=? AND c.deleted!=? ORDER BY created_time DESC")
												 ->limit(1)
												 ->execute($objTasks->id,'1');
				if($objLastcomment->numRows>0)
				{
					$lastcomment=$objLastcomment->username . "<br />" . date($GLOBALS['TL_CONFIG']['dateFormat'],$objLastcomment->created_date) . " " . date($GLOBALS['TL_CONFIG']['timeFormat'],$objLastcomment->created_time);
				}
				else
				{
					$lastcomment="No comment";
				}
				if($objProject->visibility="E") //Everybody can see the tasks
				{
					$show=true;
				}
				elseif($objTasks->created_user==$this->member['id']) //Current user is creator of the task
				{
					$show=true;
				}
				elseif(inarray($this->member['id'],$objTasks->owners)) //Current user is owner of the task
				{
					$show=true;
				}
				elseif(inarray($this->member['id'],$objTasks->ccs)) //Current user is in CC
				{
					$show=true;
				}
				elseif($userisadmin) //User is admin of the project
				{
					$show=true;
				}
				else
				{
					$show=false;
				}
				$arrRet[]=array(
				'id'=>$objTasks->id,
				'title'=>$objTasks->title,
				'lastcomment'=>$lastcomment,
				'comments'=>$objLastcomment->cnt_comment,
				'show'=>$show,
				'redirect'=>$this->generateFrontendUrl($objTargetPage->row(),'/task/' . $objTasks->id)
				);
		}
		$this->Template->tasks=$arrRet;
		
		$objTypes=$this->Database->prepare("SELECT * FROM tl_tm_types ORDER BY name ASC")->execute();
		while($objTypes->next()){
			$arrTypes[]=array(
				'id'=>$objTypes->id,
				'name'=>$objTypes->name
			);
		}
		$this->Template->types=$arrTypes;
		
		$this->Template->projectid=$projectid;
		
		
	}//compile
	
}

?>